package xrjson

import (
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/os/glog"
	"xr-game-server/core/cfg"
)

type LoadJsonHandler func(jsonData string)

var loadJsonHandlerMap = make(map[string]LoadJsonHandler)

var tempData = make(map[string]string)

// RegLoadJsonHandler 注册json加载处理器
func RegLoadJsonHandler(jsonName string, loadJsonHandler LoadJsonHandler) {
	loadJsonHandlerMap[jsonName] = loadJsonHandler
}

func Init() {
	ctx := gctx.New()
	for jName, handler := range loadJsonHandlerMap {
		data := gfile.GetContents(cfg.GetServerCfg().JsonResPath + jName)
		if len(data) == 0 {
			glog.Errorf(ctx, "%s json 无法加载到数据", jName)
			break
		}
		glog.Warningf(ctx, "%s  数据加载 %s", jName, data)
		handler(data)
	}

}
